package com.stock.demo.function.home.presenter;

import com.lzy.okgo.OkGo;
import com.stock.demo.framework.net.JsonCallback;
import com.stock.demo.function.home.view.HomeFragment;
import com.stock.demo.function.home.view.HomeView;
import com.stock.demo.model.HomeBannerBean;
import com.stock.demo.util.Urls;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by XuZhaoyu on 2017/6/25.
 */

public class HomePresenterImpl implements HomePresenter {

    private HomeView mHomeView;

    public HomePresenterImpl(HomeView homeView) {
        mHomeView = homeView;
    }

    @Override
    public void getBanner() {
        OkGo.post(Urls.BASE_BANNER_URL + Urls.HOME_BANNER_URL)
                .tag(this)
                .execute(new JsonCallback<HomeBannerBean>() {
                    @Override
                    public void onSuccess(HomeBannerBean bean, Call call, Response response) {
                        if (mHomeView instanceof HomeFragment) {
                            ((HomeFragment) mHomeView).onNetNext(bean).setNetComplete();
                            if (bean.isSuccess()) {
                                mHomeView.onGetBanner(bean.getEntity().getIndexCenterBanner());
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        if (mHomeView instanceof HomeFragment) {
                            ((HomeFragment) mHomeView).setNetError();
                        }
                    }
                });
    }

}
